package com.zdd.chap4a.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdd.chap4a.entity.Book;
import com.zdd.chap4a.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    //查找所有图书
    @GetMapping("/books")
    public ModelAndView findAllBooks(){
        List<Book> books=bookService.findAllBooks();
        ModelAndView mv=new ModelAndView();
        mv.addObject("books",books);
        mv.setViewName("booklist");
        return mv;
    }

    //动态查询书籍
    @GetMapping("/searchBooks")
    public ModelAndView searchBooks(Book book){
        List<Book> books=bookService.searchBooks(book);
        ModelAndView mv=new ModelAndView();
        mv.addObject("books",books);
        mv.setViewName("booklist");
        return mv;
    }

    //分页查询
    @GetMapping("/booksPage")
    public ModelAndView booksPage(@RequestParam(value = "start",defaultValue = "1") int start,@RequestParam(value = "size",defaultValue = "3") int size){
        //默认查询第11页，每页显示3条数据
        //表示起始页为start，每页显示size条并根据id升序排序进行分页
        PageHelper.startPage(start,size,"id asc");
        List<Book> list=bookService.findAllBooks();
        PageInfo<Book> page=new PageInfo<>(list);
        System.out.println(page.getPageNum());
        System.out.println(page.getPageSize());
        ModelAndView mv=new ModelAndView();
        mv.addObject("page",page);
        mv.setViewName("booksListPage");
        return mv;
    }

    //查找指定编号的图书
    @GetMapping("/book/{id}")
    public ModelAndView findBookById(@PathVariable("id")int id){
        Book book=bookService.findBookById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("book",book);
        mv.setViewName("bookdetail");
        return mv;
    }

    //添加新书
    @GetMapping("/addBook")
    public String addBook(){
        return "addbook";
    }

    //添加新书
    @PostMapping("/addBook")
    public ModelAndView addBook(Book book){
        bookService.addBook(book);
        ModelAndView mv=new ModelAndView();
        //添加成功后跳转到查找所有书籍的控制器
        mv.setViewName("redirect:/books");
        return mv;
    }

    //根据编号删除图书
    @GetMapping("/deleteBook/{id}")
    public ModelAndView deleteBook(@PathVariable("id") int id){
        bookService.deleteBook(id);
        ModelAndView mv=new ModelAndView();
        //删除成功后跳转到查找书籍的控制器
        mv.setViewName("redirect:/books");
        return mv;
    }

    //批量删除
    @PostMapping("/deleteBooks")
    public ModelAndView deleteBooks(int[] id){
        System.out.println(1);
        bookService.deleteBooks(id);
        ModelAndView mv=new ModelAndView();
        //删除成功后，直接跳转至查询所有书籍
        mv.setViewName("redirect:/books");
        return mv;
    }

    //去修改图书界面
    @GetMapping("/toUpdateBook/{id}")
    public ModelAndView toUpdateBook(@PathVariable("id")int id){
        Book book=bookService.findBookById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("book",book);
        mv.setViewName("updateBook");
        return mv;
    }

    //修改图书
    @PostMapping("/updateBook")
    public ModelAndView updateBook(Book book){
        bookService.updateBook(book);
        ModelAndView mv=new ModelAndView();
        //修改成功后跳转至查找所有书籍的控制器
        mv.setViewName("redirect:/books");
        return mv;
    }
}
